package com.jzx.provider.cv.controller;

import com.hankcs.hanlp.HanLP;
import com.jzx.provider.common.constants.HttpHeader;
import com.jzx.provider.common.result.InvokeResult;
import com.jzx.provider.common.result.PageResult;
import com.jzx.provider.cv.dto.CvPage;
import com.jzx.provider.cv.dto.command.*;
import com.jzx.provider.cv.dto.query.CvQueryForDetail;
import com.jzx.provider.cv.dto.query.CvQueryForIndex;
import com.jzx.provider.cv.dto.result.CvResult;
import com.jzx.provider.cv.dto.result.CvResultForIndex;
import com.jzx.provider.cv.dto.result.CvResultForNoContact;
import com.jzx.provider.cv.dto.result.FindTopManagerResult;
import com.jzx.provider.cv.service.CvIndexService;
import com.jzx.provider.cv.service.CvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author daihuihui
 * @date 2019/2/5
 */
@Validated
@RestController
@RequestMapping("/cv")
public class CvController {
    @Autowired
    private CvService cvService;

    @Autowired
    private CvIndexService cvIndexService;


    @PostMapping("/findCvIndexByQuery")
    public PageResult<List<CvResultForIndex>> findCvIndexByQuery(@RequestBody CvQueryForIndex cvQueryForIndex) {
         CvPage<CvResultForIndex> cvResultForIndices = cvIndexService.findCvIndexByQuery(cvQueryForIndex);
        return PageResult.success(cvResultForIndices.getContent(), cvResultForIndices.getTotal(), cvResultForIndices.getHighlight());
    }

    @PostMapping("/saveOrUpdate")
    public InvokeResult<Long> saveOrUpdate(@RequestBody @Validated CvCommand cvCommand
            , @RequestHeader(HttpHeader.TOKEN) String token) {
        return InvokeResult.success(cvService.saveOrUpdate(cvCommand, token));
    }

    /**
     * @param key
     * @return
     * @RequestHeader(HttpHeader.TOKEN) String token
     */
    @GetMapping("/test/{key}")
    public InvokeResult<Void> test(@PathVariable String key) {
        HanLP.segment(key.toLowerCase());
        return InvokeResult.success();
    }

    @PostMapping("/saveOrUpdateCurrentInfo")
    public InvokeResult<Void> saveOrUpdateCurrentInfo(@RequestBody @Validated CurrentInfoCommand currentInfoCommand) {
        cvService.saveOrUpdateCurrentInfo(currentInfoCommand);
        return InvokeResult.success();
    }

    @PostMapping("/saveOrUpdateForwardInfo")
    public InvokeResult<Void> saveOrUpdateForwardInfo(@RequestBody @Validated ForwardInfoCommand forwardInfoCommand) {
        cvService.saveOrUpdateForwardInfo(forwardInfoCommand);
        return InvokeResult.success();
    }

    @PostMapping("/findCvById")
    public InvokeResult<CvResultForNoContact> findCvById(@RequestBody CvQueryForDetail cvQueryForDetail) {
        return InvokeResult.success(cvService.findCvById(cvQueryForDetail.getCvId(), cvQueryForDetail.getHighlight()));
    }

    @PostMapping("/addCv")
    @ResponseBody
    public InvokeResult addCv(@RequestBody @Validated AddCvCommand addCvCommand) {
        cvService.addCv(addCvCommand);
        return InvokeResult.success();
    }

    @PostMapping("/updateCvAndCurrent")
    public InvokeResult updateCvAndCurrent(@RequestBody @Validated UpdateCvAndCurrentCommand updateCvAndCurrentCommand) {
        cvService.updateCvAndCurrent(updateCvAndCurrentCommand);
        return InvokeResult.success();
    }

    @PostMapping("/updateCvPersonal")
    public InvokeResult updateCvPersonal(@RequestBody @Validated UpdateCvAndCurrentCommand updateCvAndCurrentCommand) {
        cvService.updateCvPersonal(updateCvAndCurrentCommand);
        return InvokeResult.success();
    }

    @PostMapping("/updateForwardInfo")
    public InvokeResult updateForwardInfo(@RequestBody @Validated ForwardInfoCommand forwardInfoCommand) {
        cvService.updateForwardInfo(forwardInfoCommand);
        return InvokeResult.success();
    }

    @PostMapping("/updateCerInfo")
    public InvokeResult updateCerInfo(@RequestBody @Validated CerInfoCommand cerInfoCommand) {
        cvService.updateCerInfo(cerInfoCommand);
        return InvokeResult.success();
    }

    @PostMapping("/findCvByNameAndMobile")
    public InvokeResult<CvResult> findCvByNameAndMobile(@RequestBody @Validated FindCvCommand findCvCommand) {
        return InvokeResult.success(cvService.findCvByNameAndMobile(findCvCommand.getName(), findCvCommand.getTel()));
    }

    @GetMapping("/deleteCer/{id}")
    public InvokeResult<Void> deleteCer(@PathVariable Integer id) {
        cvService.deleteCer(id);
        return InvokeResult.success();
    }
}
